package org.daochong.web;

import java.util.Collections;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;

import org.daochong.lang.session.Session;
import org.daochong.lang.session.SessionContextFactory;
import org.daochong.web.WebUtils;

@SuppressWarnings("all")
public class SessionRequestWrapper extends HttpServletRequestWrapper {

	private HttpSession session;

	public SessionRequestWrapper(HttpServletRequest request, HttpServletResponse response) {
		super(request);
		this.session = new HttpSessionWapper(request, response);
	}

	
	public HttpSession getSession(boolean create) {
		return this.session;
	}

	
	public HttpSession getSession() {
		return this.session;
	}

}

class HttpSessionWapper implements HttpSession {
	private Session session;
	private HttpServletRequest request;

	public HttpSessionWapper(HttpServletRequest request, HttpServletResponse response) {
		HttpSession s = request.getSession(true);
		String token = (String) s.getAttribute(WebUtils.TOKEN);
		if (token == null) {
			token = WebUtils.getToken(request, response);
			s.setAttribute(WebUtils.TOKEN, token);
		}
		this.session = SessionContextFactory.getInstance().getSession(token);
		this.request = request;
	}

	public HttpSessionWapper(String sessionId) {
		this.session = SessionContextFactory.getInstance().getSession(sessionId);
	}

	
	public long getCreationTime() {
		return this.session.getCreateTime();
	}

	
	public String getId() {
		return this.session.getSessionId();
	}

	
	public long getLastAccessedTime() {
		return this.session.getLastAccessedTime();
	}

	
	public ServletContext getServletContext() {
		return request.getServletContext();
	}

	
	public void setMaxInactiveInterval(int interval) {
		SessionContextFactory.getInstance().setMaxInactiveInterval(interval);
	}

	
	public int getMaxInactiveInterval() {
		return this.session.getMaxInactiveInterval();
	}

	
	@SuppressWarnings("all")
	public HttpSessionContext getSessionContext() {
		return null;
	}

	
	public Object getAttribute(String name) {
		return this.session.getAttribute(name);
	}

	
	public Object getValue(String name) {
		return getAttribute(name);
	}

	
	public Enumeration<String> getAttributeNames() {
		return Collections.enumeration(this.session.getAttributeNames());
	}

	
	public String[] getValueNames() {
		return this.session.getAttributeNames().toArray(new String[] {});
	}

	
	public void setAttribute(String name, Object value) {
		this.session.setAttribute(name, value);
	}

	
	public void putValue(String name, Object value) {
		setAttribute(name, value);
	}

	
	public void removeAttribute(String name) {
		this.session.remove(name);
	}

	
	public void removeValue(String name) {
		removeAttribute(name);
	}

	
	public void invalidate() {
		this.session.invalidate();
	}

	
	public boolean isNew() {
		return this.session.isNewSession();
	}

}

@SuppressWarnings("all")
class HttpSessionContextWrapper implements HttpSessionContext {

	
	public HttpSession getSession(String sessionId) {
		return new HttpSessionWapper(sessionId);
	}

	
	public Enumeration<String> getIds() {
		return Collections.enumeration(SessionContextFactory.getInstance().getSessionKeys());
	}

}